package cn.humaohua.study.hadoop.cmdrunner.c3;

import cn.humaohua.study.hadoop.cmdrunner.Test;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
//https://blog.csdn.net/map_lixiupeng/article/details/32153251
@Component
public class FileSystemAppend implements Test {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

    @Override
    public void test() throws Exception {
        String dst = "hdfs://localhost/user/hmh/sample_to_delete.txt";
        InputStream in = new ByteArrayInputStream(getNow().getBytes());
        Configuration conf = new Configuration();
//        conf.setBoolean("dfs.support.append", true);//no use??
        conf.set("dfs.client.block.write.replace-datanode-on-failure.policy", "NEVER");
        conf.set("dfs.client.block.write.replace-datanode-on-failure.enable", "true");
        FileSystem fs = FileSystem.get(URI.create(dst), conf);
        OutputStream out = fs.append(new Path(dst));
        IOUtils.copyBytes(in, out, 4096, true);
    }

    private String getNow() {
        return simpleDateFormat.format(new Date())+"\r\n";
    }
}
